﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.FluentBuilder.DefaultSettingsBuilder
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using MCM.Abstractions;
using MCM.Abstractions.Base.Global;
using MCM.Abstractions.Base.PerCampaign;
using MCM.Abstractions.Base.PerSave;
using MCM.Abstractions.FluentBuilder;
using MCM.Abstractions.Wrapper;
using MCM.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;

#nullable enable
namespace MCM.Implementation.FluentBuilder
{
  internal sealed class DefaultSettingsBuilder : BaseSettingsBuilder
  {
    private Dictionary<string, ISettingsPropertyGroupBuilder> PropertyGroups { get; } = new Dictionary<string, ISettingsPropertyGroupBuilder>();

    private Dictionary<string, ISettingsPresetBuilder> Presets { get; } = new Dictionary<string, ISettingsPresetBuilder>();

    private string Id { get; }

    private string DisplayName { get; }

    private string FolderName { get; set; } = string.Empty;

    private string SubFolder { get; set; } = string.Empty;

    private string Format { get; set; } = "memory";

    private int UIVersion { get; set; } = 1;

    private char SubGroupDelimiter { get; set; } = '/';

    private PropertyChangedEventHandler? OnPropertyChanged { get; set; }

    public DefaultSettingsBuilder(string id, string displayName)
    {
      this.Id = id;
      this.DisplayName = displayName;
      this.CreateGroup(SettingsPropertyGroupDefinition.DefaultGroupName, (Action<ISettingsPropertyGroupBuilder>) (_ => { }));
      this.CreatePreset("default", "{=BaseSettings_Default}Default", (Action<ISettingsPresetBuilder>) (_ => { }));
    }

    public override ISettingsBuilder SetFolderName(string value)
    {
      this.FolderName = value;
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder SetSubFolder(string value)
    {
      this.SubFolder = value;
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder SetFormat(string value)
    {
      this.Format = value;
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder SetUIVersion(int value)
    {
      this.UIVersion = value;
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder SetSubGroupDelimiter(char value)
    {
      this.SubGroupDelimiter = value;
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder SetOnPropertyChanged(PropertyChangedEventHandler value)
    {
      this.OnPropertyChanged = value;
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder CreateGroup(
      string name,
      Action<ISettingsPropertyGroupBuilder> builder)
    {
      if (!this.PropertyGroups.ContainsKey(name))
        this.PropertyGroups[name] = (ISettingsPropertyGroupBuilder) new DefaultSettingsPropertyGroupBuilder(name);
      builder(this.PropertyGroups[name]);
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder CreatePreset(
      string id,
      string name,
      Action<ISettingsPresetBuilder> builder)
    {
      if (!this.Presets.ContainsKey(id))
        this.Presets[id] = (ISettingsPresetBuilder) new DefaultSettingsPresetBuilder(id, name);
      builder(this.Presets[id]);
      return (ISettingsBuilder) this;
    }

    public override ISettingsBuilder WithoutDefaultPreset()
    {
      this.Presets.Remove("default");
      return (ISettingsBuilder) this;
    }

    public override FluentGlobalSettings BuildAsGlobal()
    {
      ISettingsPresetBuilder settingsPresetBuilder;
      if (this.Presets.TryGetValue("default", out settingsPresetBuilder))
      {
        foreach (SettingsPropertyDefinition settingProperty in this.GetSettingProperties())
          settingsPresetBuilder.SetPropertyValue(settingProperty.Id, settingProperty.PropertyReference.Value);
      }
      return new FluentGlobalSettings(this.Id, this.DisplayName, this.FolderName, this.SubFolder, this.Format, this.UIVersion, this.SubGroupDelimiter, this.OnPropertyChanged, this.GetSettingPropertyGroups(), (IEnumerable<ISettingsPresetBuilder>) this.Presets.Values);
    }

    public override FluentPerSaveSettings BuildAsPerSave()
    {
      ISettingsPresetBuilder settingsPresetBuilder;
      if (this.Presets.TryGetValue("default", out settingsPresetBuilder))
      {
        foreach (SettingsPropertyDefinition settingProperty in this.GetSettingProperties())
          settingsPresetBuilder.SetPropertyValue(settingProperty.Id, settingProperty.PropertyReference.Value);
      }
      return new FluentPerSaveSettings(this.Id, this.DisplayName, this.FolderName, this.SubFolder, this.UIVersion, this.SubGroupDelimiter, this.OnPropertyChanged, this.GetSettingPropertyGroups(), (IEnumerable<ISettingsPresetBuilder>) this.Presets.Values);
    }

    public override FluentPerCampaignSettings BuildAsPerCampaign()
    {
      ISettingsPresetBuilder settingsPresetBuilder;
      if (this.Presets.TryGetValue("default", out settingsPresetBuilder))
      {
        foreach (SettingsPropertyDefinition settingProperty in this.GetSettingProperties())
          settingsPresetBuilder.SetPropertyValue(settingProperty.Id, settingProperty.PropertyReference.Value);
      }
      return new FluentPerCampaignSettings(this.Id, this.DisplayName, this.FolderName, this.SubFolder, this.UIVersion, this.SubGroupDelimiter, this.OnPropertyChanged, this.GetSettingPropertyGroups(), (IEnumerable<ISettingsPresetBuilder>) this.Presets.Values);
    }

    private IEnumerable<SettingsPropertyGroupDefinition> GetSettingPropertyGroups()
    {
      return (IEnumerable<SettingsPropertyGroupDefinition>) SettingsUtils.GetSettingsPropertyGroups(this.SubGroupDelimiter, (IEnumerable<ISettingsPropertyDefinition>) this.GetSettingProperties());
    }

    private IEnumerable<SettingsPropertyDefinition> GetSettingProperties()
    {
      foreach (ISettingsPropertyGroupBuilder settingsPropertyGroup in this.PropertyGroups.Values)
      {
        IPropertyGroupDefinition groupDefinition = settingsPropertyGroup.GetPropertyGroupDefinition();
        yield return new SettingsPropertyDefinition((IEnumerable<IPropertyDefinitionBase>) new \u003C\u003Ez__ReadOnlySingleElementList<IPropertyDefinitionBase>((IPropertyDefinitionBase) new PropertyDefinitionGroupMetadataWrapper((object) new
        {
          DisplayName = "",
          Order = 0,
          RequireRestart = false,
          HintText = ""
        })), (IPropertyGroupDefinition) new PropertyGroupDefinitionWrapper((object) groupDefinition), (IRef) new StorageRef<object>(new object()), this.SubGroupDelimiter);
        foreach (ISettingsPropertyBuilder settingsProperty in settingsPropertyGroup.Properties.Values)
          yield return new SettingsPropertyDefinition(settingsProperty.GetDefinitions(), (IPropertyGroupDefinition) new PropertyGroupDefinitionWrapper((object) groupDefinition), settingsProperty.PropertyReference, this.SubGroupDelimiter);
        groupDefinition = (IPropertyGroupDefinition) null;
      }
    }
  }
}
